import { Body, Query,Controller, Get ,Post} from '@nestjs/common';
import { AppService } from './app.service';
import { CacheService } from './redis/cache.service';

// @Request()	req
// @Response()	res
// @Next()	next
// @Session()	req.session
// @Param(key?: string)	req.params / req.params[key]
// @Body(key?: string)	req.body / req.body[key]
// @Query(key?: string)	req.query / req.query[key]
// @Headers(name?: string)	req.headers / req.headers[name]

@Controller()
export class AppController {
  constructor(private CacheService: CacheService,private readonly appService: AppService) {}

  // @Get()
  // getHello(): string {
  //   this.CacheService.set('user',[{ username: 'admin', password: '1234' }]);
  //   return this.appService.getHello();
  // }
  @Get()
  getH(): string {
    this.CacheService.get('user').then(data=>{
      console.log(data)
    })
    return this.appService.getHello();
  }
  @Post('/login')
  async get1( @Body() body ,@Query() query): Promise<object >  {
    console.log('login-',body,query)
    let UserArr = await this.CacheService.get('user');
    for(let i=0 ; i<UserArr.length; i++){
      console.log(UserArr[i])
      if( UserArr[i].username == body.username
        && UserArr[i].password == body.password){
          return this.appService.login(true);
        }
    }
    return this.appService.login(false);
  }
  @Post('/zhuce')
  async get2( @Body() body  ): Promise<object >  {
    console.log('zhuce-',body)
    if(!body.username||!body.password){
      return this.appService.zhuce(false);
    }
    let UserArr = await this.CacheService.get('user');
    UserArr.push(body);
    console.log(UserArr)
    await this.CacheService.set('user',UserArr);
    return this.appService.zhuce(true);
  }
}
